package com.shine.level.easy;

/**
 * 整数反转
 * //输入：x = 123
 * //输出：321
 * //输入：x = -123
 * //输出：-321
 * @author zhangshao
 * @date 2021/6/15 3:16 下午
 */
public class ReverseInteger {

    public static int reverse(int x){
        //防止-reverse(-x)出错
        if(x == Integer.MIN_VALUE){
            return 0;
        }
        if(x<0){
            return -reverse(-x);
        }
        int res = 0;
        while (x!=0){
            if(res > 214748364){
                return 0;
            }
            int temp = x % 10;
            res = res * 10 + temp;
            x = x/10;
        }
        return res ;
    }

    public static void main(String[] args) {
        System.out.println(reverse(123));
    }
}
